package com.ruoyi.system.controller;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.vo.DriverVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.system.domain.Driver;
import com.ruoyi.system.service.IDriverService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 司机管理Controller
 *
 * @author trip
 * @date 2024-09-10
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/driver")
public class DriverController extends BaseController {

    private final IDriverService driverService;

/**
 * 查询司机管理列表
 */
@PreAuthorize("@ss.hasPermi('system:driver:list')")
@GetMapping("/list")
    public TableDataInfo list(Driver driver) {
        startPage();
    List<DriverVO> list = driverService.getDriverList(driver);
        return getDataTable(list);
    }

    /**
     * 下载导入模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Driver> util = new ExcelUtil<Driver>(Driver.class);
        util.importTemplateExcel(response, "司机管理数据");
    }

    /**
     * 导入司机管理数据
     * @param file
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('system:driver:import')")
    @PostMapping("/importData")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Driver> util = new ExcelUtil<Driver>(Driver.class);
        List<Driver> list = util.importExcel(file.getInputStream());

        if (list.size() <= 0) {
            throw new RuntimeException("导入数据为空,请填充数据再导入！");
        }
        // 批量存入
            driverService.saveBatch(list);

        return AjaxResult.success("导入成功");
    }

    /**
     * 导出司机管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:driver:export')")
    @Log(title = "司机管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Driver driver) {
        List<Driver> list = driverService.selectDriverList(driver);
        ExcelUtil<Driver> util = new ExcelUtil<Driver>(Driver.class);
        util.exportExcel(response, list, "司机管理数据");
    }

    /**
     * 获取司机管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:driver:query')")
    @GetMapping(value = "/{driverId}")
    public AjaxResult getInfo(@PathVariable("driverId") Long driverId) {
        return AjaxResult.success(driverService.selectDriverByDriverId(driverId));
    }

    /**
     * 新增司机管理
     */
    @PreAuthorize("@ss.hasPermi('system:driver:add')")
    @Log(title = "司机管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Driver driver)  {
        return toAjax(driverService.insertDriver(driver));
    }

    /**
     * 修改司机管理
     */
    @PreAuthorize("@ss.hasPermi('system:driver:edit')")
    @Log(title = "司机管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Driver driver)  {
        return toAjax(driverService.updateDriver(driver));
    }

    /**
     * 删除司机管理
     */
    @PreAuthorize("@ss.hasPermi('system:driver:remove')")
    @Log(title = "司机管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{driverIds}")
    public AjaxResult remove(@PathVariable Long[] driverIds) {
        return toAjax(driverService.deleteDriverByDriverIds(driverIds));
    }
}


